package com.egoo.multipartfilestarter.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.unit.DataSize;
import org.springframework.util.unit.DataUnit;

import javax.servlet.MultipartConfigElement;

/**
 * @Author hujian
 * @Date 2022/7/30 15:29
 * @Version 0.1
 */

@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(MultipartConfigProperties.class)
public class MultipartConfigElementAutoConfig {

    @Autowired
    MultipartConfigProperties multipartConfigProperties;

    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        // 单个文件最大值 单位 MB
        factory.setMaxFileSize(DataSize.of(multipartConfigProperties.getSetMaxFileSize(), DataUnit.MEGABYTES));
        // 设置总上传数据总大小 单位G
        factory.setMaxRequestSize(DataSize.of(multipartConfigProperties.getSetMaxRequestSize(), DataUnit.GIGABYTES));
        System.out.println("MultipartConfigElementAutoConfig init success！！！");
        return factory.createMultipartConfig();
    }


}
